<?php
/**
 * 猫眼接口
 */
namespace app\web\service;
use think\Controller;
class MaoyanApi extends Controller{
    protected $url = MYAN_URL;
    protected $key = MYAN_KEY;
    protected $externalPlatformId = 117;
    protected $pub_param = [
        'merCode' => '1000406',
        'timestamp' => '',
        'version' => '1.0',
        'signType' => 'MD5',
        'api' => '',
    ];
    /**
     * 会员开卡
     */
    public function gatewayMemberCreate($params)
    {
        if ($params) {
            $params_array = $this->pub_param;
            $params_array['api'] = 'gateway.member.create';
            $params_array['timestamp'] = time();
            // $params_array['timestamp'] = request_curl('get',$this->url.'/serverTime');
            $bizData = [
                'orderTime' => $this->timeMillisecond(),
                'mobileNo' => $params['mobileNo'],
                'lifeCycle' => $params['lifeCycle'],
                'externalPlatformId' => $this->externalPlatformId,
                'channelOrderId' => $params['channelOrderId'],
                'actionType' => $params['actionType'],
                'payMoney' => isset($params['payMoney'])?$params['payMoney']:0,
            ];
            $params_array['bizData'] = json_encode($bizData);
            // halt($params_array);
            $params_array['signMsg'] = strtoupper($this->ASCII($params_array));//签名值MD5
            return request_curl('post',$this->url.'/member/gateway',$params_array);
        } else {
            return ['code'=>-1,'message'=>'缺少参数'];
        }
    }
    /**
     * 根据外部订单号查询会员权益情况接口
     */
    public function couponInfo($params)
    {
        if ($params) {
            $params_array = $this->pub_param;
            $params_array['api'] = 'gateway.member.couponInfo';
            $params_array['timestamp'] = time();
            // $params_array['timestamp'] = request_curl('get',$this->url.'/serverTime');
            $bizData = [
                'externalPlatformId' => $this->externalPlatformId,
                'channelOrderId' => $params['channelOrderId'],
            ];
            $params_array['bizData'] = json_encode($bizData);
            // halt($params_array);
            $params_array['signMsg'] = strtoupper($this->ASCII($params_array));//签名值MD5
            return request_curl('post',$this->url.'/member/gateway',$params_array);
        } else {
            return ['code'=>-1,'message'=>'缺少参数'];
        }
    }
    /**
     * ASCII 升序
     */
    function ASCII($params = array()){
		//ksort()对数组按照键名进行升序排序
		ksort($params);
        //reset()内部指针指向数组中的第一个元素
		reset($params);
		$str = http_build_query($params).'&key='. $this->key;
        $str = urldecode($str);
        // echo $str.'<br>';
		return md5($str);
	}
    /**
     * 获取当前时间的时间戳毫秒级
     * */
    function timeMillisecond(){
    	list($msec, $sec) = explode(' ', microtime());
    	$s = floatval($msec) + floatval($sec);
    	$time = sprintf('%.0f', $s*1000);
    	return $time;
    }
}